/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "LoggerGenerator.hpp"

LoggerGenerator::LoggerGenerator(gimi::GIMI *aGimi): iGIMI(aGimi)
{
  dPrint(1,"Logger Generator constructed");
}
LoggerGenerator::~LoggerGenerator(){

}

Logger *LoggerGenerator::GenerateNewLogger(int32_t aMajorType, int32_t aMinorType,
                                           const unsigned int aTargetID,std::string aMaCISLName){

  Logger *logger = NULL;
  dPrint(1,"Generating %d:%d id:%u MaCISL:%s",aMajorType,aMinorType,aTargetID,aMaCISLName.c_str());
  switch(aMajorType){

  case GIMI_PROTOCOL_MACI_RANGING:{
    ///////////////////////////////////////////////////////////////
    ////////// Ranging
    ///////////////////////////////////////////////////////////////
    dPrint(6,"Constructing ranging");
    MaCI::Ranging::CRangingClient *rangingClient = new MaCI::Ranging::CRangingClient(iGIMI,aMinorType);
    rangingClient->SetDefaultTarget(aTargetID);
    
    if (rangingClient->Open()==MaCI::KMaCIOK){
      dPrint(3,"Ranging opened");
      logger= new RangingLogger(rangingClient,aMaCISLName);
          
      
    } else {
      std::string s = "Could not open Ranging client";
      dPrint(1,"%s",s.c_str());
      delete rangingClient;
    }
    break;
    
  }
  case  GIMI_PROTOCOL_MACI_POSITION:{
    ///////////////////////////////////////////////////////////////
    ////////// Position
    ///////////////////////////////////////////////////////////////
    dPrint(6,"Constructing position");
    MaCI::Position::CPositionClient *positionClient = new MaCI::Position::CPositionClient(iGIMI,aMinorType);
    positionClient->SetDefaultTarget(aTargetID);
    if (positionClient->Open()==MaCI::KMaCIOK){
      logger= new PositionLogger(positionClient,aMaCISLName);
    } else {
      std::string s = "Could not open Position client";
      
      dPrint(1,"%s",s.c_str());
      delete positionClient;
    }
    break;
  }
  case GIMI_PROTOCOL_MACI_SPEEDCTRL:{
    ///////////////////////////////////////////////////////////////
    ////////// SpeedCTRL
    ///////////////////////////////////////////////////////////////
    dPrint(6,"Constructing speedctrl");
    MaCI::SpeedCtrl::CSpeedCtrlClient *speedCtrlClient = new MaCI::SpeedCtrl::CSpeedCtrlClient(iGIMI,aMinorType);
    speedCtrlClient->SetDefaultTarget(aTargetID);
    if (speedCtrlClient->Open()==MaCI::KMaCIOK){
      
      logger= new SpeedCtrlLogger(speedCtrlClient,aMaCISLName);
      
      
    } else {
      std::string s = "Could not open SpeedCtrl client";
      dPrint(1,"%s",s.c_str());
      
      delete speedCtrlClient;
    }
    break;
  }
  case  GIMI_PROTOCOL_MACI_IMU:{
    ///////////////////////////////////////////////////////////////
    ////////// IMU
    ///////////////////////////////////////////////////////////////        
    dPrint(6,"Constructing IMU");
    MaCI::IMU::CIMUClient *imuClient = new MaCI::IMU::CIMUClient(iGIMI,aMinorType);
    imuClient->SetDefaultTarget(aTargetID);
    if (imuClient->Open()==MaCI::KMaCIOK){
      
      logger= new IMULogger(imuClient,aMaCISLName);
          
      
    } else {
      std::string s = "Could not open IMU client";
      dPrint(1,"%s",s.c_str());
      
      delete imuClient;
    }
    break;
  }
  case GIMI_PROTOCOL_MACI_ENERGY:{
    ///////////////////////////////////////////////////////////////
        ////////// Energy
        ///////////////////////////////////////////////////////////////
    dPrint(6,"Constructing Energy");
    MaCI::Energy::CEnergyClient *energyClient = new MaCI::Energy::CEnergyClient(iGIMI,aMinorType);
    energyClient->SetDefaultTarget(aTargetID);
    if (energyClient->Open()==MaCI::KMaCIOK){
      
      logger= new EnergyLogger(energyClient,aMaCISLName);
      
      
    } else {
      std::string s = "Could not open energy client";
      dPrint(1,"%s",s.c_str());
      
      delete energyClient;
    }
    break;
  }
  case GIMI_PROTOCOL_MACI_WIRELESS:{
    ///////////////////////////////////////////////////////////////
    ////////// Wireless
    ///////////////////////////////////////////////////////////////
    dPrint(6,"Constructing Wireless");
    MaCI::Wireless::CWirelessClient *wirelessClient = new MaCI::Wireless::CWirelessClient(iGIMI, aMinorType);
    wirelessClient->SetDefaultTarget(aTargetID);
    
    if (wirelessClient->Open() == MaCI::KMaCIOK) {
      logger= new WirelessLogger(wirelessClient,aMaCISLName);
      
    } else {
      std::string s = "Could not open wireless client";
      dPrint(1,"%s",s.c_str());
      
      delete wirelessClient;
    } 
    break;
  }
  case GIMI_PROTOCOL_MACI_COORDINATEDRIVE:{
    ///////////////////////////////////////////////////////////////
    ////////// CoordinateDrive
    ///////////////////////////////////////////////////////////////
    dPrint(6,"Constructing CoordinateDrive");
    MaCI::CoordinateDrive::CCoordinateDriveClient *coordinateDriveClient = new MaCI::CoordinateDrive::CCoordinateDriveClient(iGIMI, aMinorType);
    coordinateDriveClient->SetDefaultTarget(aTargetID);
    
    if (coordinateDriveClient->Open() == MaCI::KMaCIOK) {
      logger= new CoordinateDriveLogger(coordinateDriveClient,aMaCISLName);
    } else {
      std::string s = "Could not open CoordinateDrive client";
      dPrint(1,"%s",s.c_str());
      
      delete coordinateDriveClient;
    } 
    break;
  }
  case  GIMI_PROTOCOL_MACI_IMAGE:{
    ///////////////////////////////////////////////////////////////
    ////////// Image
    ///////////////////////////////////////////////////////////////
    dPrint(6,"Constructing Image client");
    MaCI::Image::CImageClient *imageClient = new MaCI::Image::CImageClient(iGIMI, aMinorType);
    imageClient->SetDefaultTarget(aTargetID);
    
    if (imageClient->Open() == MaCI::KMaCIOK) {
      logger= new ImageLogger(imageClient,aMaCISLName);
      
    } else {
      std::string s = "Could not open Image client";
      dPrint(1,"%s",s.c_str());
      
      delete imageClient;
    } 
    break;
    ///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////
  }
  default:{
    dPrint(6,"no can do.. can't recognize major %d",aMajorType);
    break;
  }
  }

  return logger;
};
